//
//  HYPresentationControllerManeger.swift
//  HYYWeibo
//
//  Created by HY-Mac on 16/7/10.
//  Copyright © 2016年 HY-Mac. All rights reserved.
//

import UIKit

let HYPresentationControllerManegerDidPresented = "HYPresentationControllerManegerDidPresent"
let HYPresentationControllerManegerDidDismiss = "HYPresentationControllerManegerDidDismiss"

class HYPresentationControllerManeger: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning{
    
    private var isPresented = false
    
    var presentViewFrame = CGRectZero
    
    //MARK: -UIViewControllerTransitioningDelegate
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentControlloer = HYPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presentControlloer.presentViewFrame = presentViewFrame
        return presentControlloer
    }
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        NSNotificationCenter.defaultCenter().postNotificationName(HYPresentationControllerManegerDidPresented, object: self)
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        NSNotificationCenter.defaultCenter().postNotificationName(HYPresentationControllerManegerDidDismiss, object: self)
        return self
    }
    
    //MARK: -UIViewControllerAnimatedTransitioning
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
  
        if isPresented{
            ControllerWillPresent(transitionContext)
        }
        else{
            ControllerWillDismiss(transitionContext)
            }
    }
    
    
    
    private func ControllerWillPresent(transitionContext: UIViewControllerContextTransitioning){
        guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else{
            return
        }
        transitionContext.containerView()?.addSubview(toView)
            
        //commit animation
            
        toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            
        // set anchor point
        toView.layer.anchorPoint = CGPointMake(0.5, 0.0)
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            toView.transform =  CGAffineTransformIdentity
        }) { (_) in
            transitionContext.completeTransition(true)
            }
        }
    private func ControllerWillDismiss(transitionContext: UIViewControllerContextTransitioning){
        guard let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) else{
            return
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
            }, completion: { (_) in
                transitionContext.completeTransition(true)
        })
    }
}
